package com.hafiery.sdk.bluetooth.test.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;
import com.google.android.material.switchmaterial.SwitchMaterial;
import com.hafiery.sdk.bluetooth.test.R;

public class SettingItem extends LinearLayoutCompat {
    public TextView tailText;
    public SwitchMaterial tailSwitch;
    public SettingItem(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public SettingItem(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SettingItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context,AttributeSet attrs){
        setOrientation(HORIZONTAL);
        setMinimumHeight(getResources().getDimensionPixelSize(R.dimen.settingsItemMinHeight));
        int hPadding=getResources().getDimensionPixelSize(R.dimen.deviceItemPaddingHorizontal);
        int vPadding=getResources().getDimensionPixelSize(R.dimen.settingsItemPaddingVertical);
        setPadding(hPadding, vPadding, hPadding, vPadding);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SettingsItem);
        String title = ta.getString(R.styleable.SettingsItem_title);
        boolean arrow = ta.getBoolean(R.styleable.SettingsItem_showArrow, false);
        boolean swit = ta.getBoolean(R.styleable.SettingsItem_showSwitch, false);
        ta.recycle();

        View parent=inflate(context, R.layout.widget_settings_item, this);
        TextView titleText=parent.findViewById(R.id.title);
        tailText=parent.findViewById(R.id.tail_text);
        ImageView arrowImage=parent.findViewById(R.id.arrow);
        tailSwitch=parent.findViewById(R.id.sw);
        titleText.setText(title);
        arrowImage.setVisibility(arrow?VISIBLE:GONE);
        tailSwitch.setVisibility(swit?VISIBLE:GONE);
    }

}
